<?php

/**
 * @Author: DEEP-2018BRTYLP\Administrator
 * @Date:   2019-03-19 15:17:42
 * @Last Modified by:   DEEP-2018BRTYLP\Administrator
 * @Last Modified time: 2019-04-05 11:38:23
 */
namespace app\api\controller;
use app\common\model\Version;
use fast\Random;
use think\Config;
use app\api\controller\Base;
/**
 * 
 */
class Upload extends Base
{
    /**
     * base64 处理图片
     * @return [type] [description]
     */
    public function processImg(){
        $base64_image_content = $_POST['file'];
        //dump(date('Ymd'));exit;
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64_image_content,$result)){
            //dump($result);
            $type = $result[2];//图片后缀
            $new_file = $_SERVER['DOCUMENT_ROOT'].'/uploads/'.date('Ymd').'/';
            if (!file_exists($new_file)) {//检查是否有该文件夹，如果没有就创建，并给予最高权限
                mkdir($new_file,0777);
            }
            $filename = time() . '_' . uniqid() . ".{$type}"; //文件名
            $new_file = $new_file . $filename;
          //写入操作
          if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
            //这里运用了阿里云的图片上传
            //$this->load->library('AliUpload');
            //$url = $this->aliupload->uploadVideo($filename,$new_file);
          }
        }
        //存储数据库
        $member_id = input('param.id',1);
        db('image')->insert(['member_id'=>$member_id,'image'=>'/uploads/'.date('Ymd').'/'.$filename,'createtime'=>time()]);
        return '/uploads/'.date('Ymd').'/'.$filename;
    }
    
}
